package com.firstgroup.dms.service.impl;

import com.firstgroup.dms.common.Result;
import com.firstgroup.dms.mapper.ProcurementMapper;
import com.firstgroup.dms.mapper.EmployeeMapper;
import com.firstgroup.dms.pojo.ProcurementInformation;
import com.firstgroup.dms.pojo.ProcurementDetail;
import com.firstgroup.dms.service.ProcurementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProcurementServiceImpl implements ProcurementService {

    @Resource
    private ProcurementMapper procurementMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private DrugInformationServiceImpl drugInformationService;

    @Override
    @Transactional
    public Result addProcurement(ProcurementInformation procurement) {
        try {
            // 保存采购单主表
            procurementMapper.insert(procurement);

            // 保存采购明细
            if (procurement.getDetails() != null && !procurement.getDetails().isEmpty()) {
                for (ProcurementDetail detail : procurement.getDetails()) {
                    detail.setPurchaseId(procurement.getId());
                    procurementMapper.insertDetail(detail);
                }
            }

            return Result.success();
        } catch (Exception e) {
            return Result.error("添加采购单失败：" + e.getMessage());
        }
    }

    @Override
    public Result getEmployees() {
        try {
            return Result.success(employeeMapper.selectAll());
        } catch (Exception e) {
            return Result.error("获取员工列表失败：" + e.getMessage());
        }
    }

    @Override
    public Result getDrugs() {
        return drugInformationService.getAllDrugs();
    }

    @Override
    public Result getDrugById(Integer id) {
        return drugInformationService.getDrugById(id);
    }

    @Override
    public Result getProcurements(String danjuhao, String jingshouren, int page, int limit) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("danjuhao", danjuhao);
            params.put("jingshouren", jingshouren);
            params.put("start", (page - 1) * limit);
            params.put("limit", limit);

            List<ProcurementInformation> list = procurementMapper.selectByPage(params);
            int total = procurementMapper.selectCount(params);

            Map<String, Object> result = new HashMap<>();
            result.put("data", list);
            result.put("count", total);

            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取采购单列表失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteProcurement(Integer id) {
        try {
            // 先删除采购明细
            procurementMapper.deleteById(id);
            // 再删除采购主表
            int rows = procurementMapper.deleteDetailsByPurchaseId(id);

            if (rows > 0) {
                return Result.success();
            } else {
                return Result.error("采购单不存在");
            }
        } catch (Exception e) {
            return Result.error("删除采购单失败：" + e.getMessage());
        }
    }

    @Override
    public Result getProcurement(Integer id) {
        try {
            ProcurementInformation procurement = procurementMapper.selectById(id);
            if (procurement == null) {
                return Result.error("采购单不存在");
            }
            return Result.success(procurement);
        } catch (Exception e) {
            return Result.error("获取采购单信息失败：" + e.getMessage());
        }
    }
}